﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace WinBookKeeping
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            DatabaseHelper.InitializeDatabase(); // 初始化数据库
        }


        private void LoginForm_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.Username))
            {
                txtUsername.Text = Properties.Settings.Default.Username;
                checkBox1.Checked = true;
                txtPassword.Focus();
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string username = txtUsername.Text.Trim();
            string password = txtPassword.Text;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入用户名和密码", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                if (ValidateUser(username, password))
                {
                    string fullName = GetUserFullName(username);
                    MessageBox.Show($"欢迎回来，{fullName}!", "登录成功", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    if (checkBox1.Checked)
                    {
                        Properties.Settings.Default.Username = username;
                        Properties.Settings.Default.Save();
                    }

                    FrmMain mainForm = new FrmMain();
                    mainForm.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("用户名或密码错误", "登录失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"登录时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private bool ValidateUser(string username, string password)
        {
            using (var connection = new SQLiteConnection(DatabaseHelper.ConnectionString))
            {
                string query = "SELECT COUNT(*) FROM Users WHERE Username=@Username AND Password=@Password";
                var command = new SQLiteCommand(query, connection);
                command.Parameters.AddWithValue("@Username", username);
                command.Parameters.AddWithValue("@Password", password);
                connection.Open();
                int count = Convert.ToInt32(command.ExecuteScalar());
                return count > 0;
            }
        }

        private string GetUserFullName(string username)
        {
            using (var connection = new SQLiteConnection(DatabaseHelper.ConnectionString))
            {
                string query = "SELECT FullName FROM Users WHERE Username=@Username";
                var command = new SQLiteCommand(query, connection);
                command.Parameters.AddWithValue("@Username", username);
                connection.Open();
                var result = command.ExecuteScalar();
                return result != null ? result.ToString() : string.Empty;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
